home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / QuickDraw / Record•RetrievePictInfo / Source / Record•RetrievePictInfo.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  4.6 KB  |  195 lines  |  [TEXT/CWIE]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    Record•RetrievePictInfo                                    */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows how to retrieve the colortable        */
  6. /*                    from multiple PICT resources using the System 7 calls    */
  7. /*                    RecordPictInfo and RetrievePictInfo.                    */
  8. /*                                                                            */
  9. /*    Files:            Record•RetrievePictInfo.π                                */
  10. /*                    Record•RetrievePictInfo.π.rsrc                            */
  11. /*                    Record•RetrievePictInfo.c                                */
  12. /*                                                                            */
  13. /*    Programmer:        Edgar Lee                                                */
  14. /*    Organization:    Apple Computer, Inc.                                    */
  15. /*    Department:        Developer Technical Support, DTS                        */
  16. /*    Language:        C (Think C version 5.0.2)                                */
  17. /*    Date Created:    10-20-92                                                */
  18. /*                                                                            */
  19. /****************************************************************************/
  20.  
  21. #include <Dialogs.h>
  22. #include <Fonts.h>
  23. #include <PictUtils.h>
  24. #include <TextUtils.h>
  25.  
  26. /* Constant Declarations */
  27.  
  28. #define    WWIDTH    512
  29. #define    WHEIGHT    200
  30.  
  31. #define    MAXPICTS    6        /* Maximum PICT resources used for the example. */
  32.  
  33. #define WLEFT    (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  34. #define WTOP    (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  35.  
  36. void initMac();
  37.  
  38. void createWindow();
  39. void retrieveColors();
  40. void doEventLoop();
  41.  
  42. void main(void)
  43. {
  44.     initMac();
  45.     
  46.     createWindow();
  47.     
  48.     doEventLoop();
  49. }
  50.  
  51. void initMac()
  52. {
  53.     MaxApplZone();
  54.  
  55.     InitGraf( &qd.thePort );
  56.     InitFonts();
  57.     InitWindows();
  58.     InitMenus();
  59.     TEInit();
  60.     InitDialogs( nil );
  61.     InitCursor();
  62.     FlushEvents( 0, everyEvent );
  63. }
  64.  
  65. void createWindow()
  66. {
  67.     Rect        rect;
  68.     WindowPtr    window;
  69.     
  70.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  71.     window = NewCWindow( 0L, &rect, "\pRecord•RetrievePictInfo", true, documentProc,
  72.                             (WindowPtr)-1L, true, 0L );                        
  73.     SetPort( window );
  74.     
  75.     TextMode( srcCopy );
  76.     TextFont( geneva );
  77.     TextSize( 9 );
  78.     
  79.     PenSize( 2, 1 );
  80. }
  81.  
  82. void retrieveColors()
  83. {
  84.     int                i;
  85.     RGBColor        color;
  86.     PicHandle        pict[MAXPICTS];
  87.     PictInfoID        pictInfoID;
  88.     PictInfo        pictInfo;
  89.     CTabHandle        ctable;
  90.     PaletteHandle    palette;
  91.     Rect            rect;
  92.     Str255            string;
  93.     
  94.     /* Request a unique ID for our PictInfo. */
  95.     NewPictInfo( &pictInfoID, returnColorTable, 256, popularMethod, 0 );
  96.     
  97.     MoveTo( 10, 30 );
  98.     DrawString( "\pRecording PictInfo" );
  99.     MoveTo( 10, 45 );
  100.     DrawString( "\pfrom PICT resources:" );
  101.     
  102.     /* Loop through the PICT resources. */
  103.     for (i = 0; i < MAXPICTS; i++)
  104.     {
  105.         /* Load the PICT. */
  106.         pict[i] = GetPicture( 128 + i );
  107.         HPurge( (Handle)pict[i] );
  108.         
  109.         /* Define the PICT's destination rect then draw into it. */
  110.         rect = (**pict[i]).picFrame;
  111.         OffsetRect( &rect, -rect.left, -rect.top );
  112.         rect.right /= 2;
  113.         rect.bottom /= 2;
  114.         
  115.         OffsetRect( &rect, rect.right * (i + 2), 0 );
  116.         DrawPicture( pict[i], &rect );
  117.         
  118.         /* Draw the PICT's ID below its image. */
  119.         MoveTo( rect.left + 20, rect.bottom + 10 );
  120.         NumToString( (long)(128 + i), string );
  121.         DrawString( string );
  122.         
  123.         /* Accumulate information about this PICT. */
  124.         RecordPictInfo( pictInfoID, pict[i] );
  125.     }
  126.  
  127.     MoveTo( 10, 95 );
  128.     DrawString( "\pColortable returned from RetrievePictInfo:" );
  129.  
  130.     /* Retrieve the colortable from the recording. */
  131.     RetrievePictInfo( pictInfoID, &pictInfo, 256 );
  132.     ctable = pictInfo.theColorTable;
  133.     
  134.     /* Convert the colortable to a palette then attach it to the window. */
  135.     palette = NewPalette( 0, nil, pmTolerant + pmExplicit, 0 );
  136.     CTab2Palette( ctable, palette, pmTolerant + pmExplicit, 0 );
  137.     SetPalette( FrontWindow(), palette, false );
  138.     
  139.     /* Draw the colortable in the window to see what it looks like. */
  140.     for (i = 0; i < 256; i++)
  141.     {
  142.         color = (**ctable).ctTable[i].rgb;
  143.         
  144.         RGBForeColor( &color );
  145.         MoveTo( i * 2, 100 );
  146.         LineTo( i * 2, WHEIGHT );
  147.     }
  148.     
  149.     /* Release any used memory. */
  150.     DisposePictInfo( pictInfoID );
  151.     DisposeCTable( ctable );
  152.     DisposePalette( palette );
  153. }
  154.  
  155. void doEventLoop()
  156. {
  157.     EventRecord event;
  158.     WindowPtr   window;
  159.     short       clickArea;
  160.     Rect        screenRect;
  161.  
  162.     for (;;)
  163.     {
  164.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  165.         {
  166.             if (event.what == mouseDown)
  167.             {
  168.                 clickArea = FindWindow( event.where, &window );
  169.                 
  170.                 if (clickArea == inDrag)
  171.                 {
  172.                     screenRect = (**GetGrayRgn()).rgnBBox;
  173.                     DragWindow( window, event.where, &screenRect );
  174.                 }
  175.                 else if (clickArea == inContent)
  176.                 {
  177.                     if (window != FrontWindow())
  178.                         SelectWindow( window );
  179.                 }
  180.                 else if (clickArea == inGoAway)
  181.                     if (TrackGoAway( window, event.where ))
  182.                         return;
  183.             }
  184.             else if (event.what == updateEvt)
  185.             {
  186.                 window = (WindowPtr)event.message;    
  187.                 SetPort( window );
  188.                 
  189.                 BeginUpdate( window );
  190.                 retrieveColors();
  191.                 EndUpdate( window );
  192.             }
  193.         }
  194.     }
  195. }